﻿#ifndef JOYSTICK2D_H
#define JOYSTICK2D_H

#include <QObject>
#include <QThread>
#include <Windows.h>
#include <Mmsystem.h>

/**
  * 2 轴 8 按键 Joystick 的驱动程序。
  * 使用的是微软的 multimedia joystick API。
  * 没有使用更流行的 DirectInput 技术。
 */
class JoyStick2D: public QThread
{
    Q_OBJECT
public:
    explicit JoyStick2D(QObject *parent = Q_NULLPTR);
    ~JoyStick2D();
    bool listen();
    void stop();
protected:
    void run() final;
signals:
    void Joy_MoveForward();
    void Joy_MoveBackward();
    void Joy_MoveForwardStop();
    void Joy_MoveBackwardStop();
    void Joy_MoveLeft();
    void Joy_MoveRight();
    void Joy_MoveLeftStop();
    void Joy_MoveRightStop();

    void Joy_POVForward();
    void Joy_POVBackward();
    void Joy_POVLeft();
    void Joy_POVRight();
    void Joy_POVForwardLeft();
    void Joy_POVForwardRight();
    void Joy_POVBackwardLeft();
    void Joy_POVBackwardRight();
    void Joy_POVCentered();

    void Joy_Button1Pressed();
    void Joy_Button2Pressed();
    void Joy_Button3Pressed();
    void Joy_Button4Pressed();
    void Joy_Button5Pressed();
    void Joy_Button6Pressed();
    void Joy_Button7Pressed();
    void Joy_Button8Pressed();
    void Joy_Button1Released();
    void Joy_Button2Released();
    void Joy_Button3Released();
    void Joy_Button4Released();
    void Joy_Button5Released();
    void Joy_Button6Released();
    void Joy_Button7Released();
    void Joy_Button8Released();

private:
    void AxisX_StateMachine(int xPos);
    void AxisY_StateMachine(int yPos);
    void Button_StateMachine(int button);
    void POV_StateMachine_Axis(int pov);
    void POV_StateMachine(int pov);
    int old_xPos;
    int old_yPos;
    int old_pov;
    bool pov_forward;
    bool pov_backward;
    bool pov_left;
    bool pov_right;
    bool pov_center;
    int m_button[16];

    bool m_run;    
    bool m_valid;
};



#endif // JOYSTICK2D_H
